package org.apselex.adaptors;

/*
 * Copyright (c) 2008 Evan Williams
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.apselex.model.ModelElement;
import org.apselex.model.ModelMetaData;
import org.apselex.model.ModelTag;
import org.apselex.providers.DataProvider;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

import apselexport.Activator;


public class ModelElementPropertySource implements IPropertySource {

	private ModelElement element;

	private HashMap<String, ModelMetaData> map = null;
	private ModelTag[] tags = null;

	private static Logger logger = Logger.getLogger(ModelElementPropertySource.class);
	
	/**
	 * @param element
	 */
	public ModelElementPropertySource(ModelElement element) {
		logger.debug("meps constructor called");
		this.element = element;
	}

	@Override
	public Object getEditableValue() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		// TODO Auto-generated method stub

		map = new HashMap<String, ModelMetaData>();

		ArrayList<IPropertyDescriptor> a = new ArrayList<IPropertyDescriptor>();

		logger.debug("in getPropertyDescriptors");

		DataProvider data = Activator.getDefault().getDataProvider();

		ModelMetaData[] meta = data.getMetaData(this.element.getId());
		logger.debug("meta returned is:" + meta);

		for (ModelMetaData o : meta) {
			logger.debug("object o is:" + o);
			String sCategory = "General";
			String sName = o.getName();

			// if (o.getDataType().equals("string")) {
			// PropertyDescriptor textDescriptor = new TextPropertyDescriptor(
			// sName, sName);
			// int iColon = o.getName().indexOf(":");
			// if (iColon > 0) {
			// sCategory = o.getName().substring(0, iColon);
			// }
			// textDescriptor.setCategory(sCategory);
			// a.add(textDescriptor);
			// }
			// if (o.getDataType().equals("integer")) {
			// PropertyDescriptor textDescriptor = new TextPropertyDescriptor(
			// o.getName(), o.getName());
			// textDescriptor.setCategory("Integer");
			// a.add(textDescriptor);
			// }
			// if (o.getDataType().equals("datetime")) {
			// PropertyDescriptor textDescriptor = new TextPropertyDescriptor(
			// o.getName(), o.getName());
			// textDescriptor.setCategory("Date Time");
			// a.add(textDescriptor);
			// }
			// if (o.getDataType().equals("decimal")) {
			// PropertyDescriptor textDescriptor = new TextPropertyDescriptor(
			// o.getName(), o.getName());
			// textDescriptor.setCategory("Decimal");
			// a.add(textDescriptor);
			// }

			PropertyDescriptor textDescriptor = new TextPropertyDescriptor(
					sName, sName);
			int iColon = sName.indexOf(":");
			if (iColon > 0) {
				sCategory = sName.substring(0, iColon);
			}
			textDescriptor.setCategory(sCategory);
			a.add(textDescriptor);

			map.put(sName, o);
		}

		tags = data.getTags(this.element.getId());
		logger.debug("meta returned is:" + tags);
		PropertyDescriptor textDescriptor = new TextPropertyDescriptor("tags",
				"Tags");

		textDescriptor.setCategory("Tags");
		a.add(textDescriptor);

		return a.toArray(new IPropertyDescriptor[a.size()]);
	}

	@Override
	public Object getPropertyValue(Object id) {

		logger.debug("getPropertyValue for:" + id);
		ModelMetaData m = map.get(id);
		if (m != null) {
			return m.getValue();
		} else {
			if (id.equals("tags")) {
				String sTags = "";
				for (ModelTag mt : tags) {
					sTags = sTags + mt.getTagName() + " ";
				}
				return sTags;
			}
		}
		return null;
	}

	@Override
	public boolean isPropertySet(Object id) {

		logger.debug("isPropertySet for:" + id);
		if (map.containsKey(id)) {
			return true;
		}

		return false;
	}

	@Override
	public void resetPropertyValue(Object id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setPropertyValue(Object id, Object value) {
		// TODO Auto-generated method stub

	}

}
